//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-11-29  17-01-24 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-11-29  17-01-24 
//===============================





//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-11-06  11-06-34 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-11-06  11-06-34 
//===============================





using System;
using System.Collections.Generic;
using UnityEngine;
using ZS.U3D.BASE.Runtime.Event;

namespace UI.BeiBao
{
    public class Area : MonoBehaviour
    {
        [SerializeField]
        private Grid _gridTemplect;

        [SerializeField]
        private int _col=3;
        [SerializeField]
        private int _row=3;


        public Grid EnterGrid;




        private List<Grid> _grids=new List<Grid>();

        private void Start()
        {
            for (int i = 0; i < _col; i++)
            {
                for (int j = 0; j < _row; j++)
                {
                    Grid grid = Instantiate(_gridTemplect, transform);
                    grid.name = $"Grid_{i}_{j}";
                    grid.gameObject.SetActive(true);
                    grid.Area = this;
                    _grids.Add(grid);

                }
            }

            EventManager.Initalize();

            EventManager.AddListener<TileDragStartEvent>(EventListenner);

            EventManager.AddListener<TileDragEndEvent>(EventListenner);

        }

        private void EventListenner(IEvent @event)
        {
            Debug.Log("EventListenner");

            TileEvent tileEvent = @event as TileEvent;

            if (@event.GetType()==typeof(TileDragStartEvent))
            {
                //Debug.Log(tileEvent.tile.name);
                tileEvent.tile.transform.SetParent(transform.parent, true);
            }
            else if (@event.GetType() == typeof(TileDragEndEvent))
            {
                //Debug.Log(tileEvent.tile.name);

                if (EnterGrid==null)
                {
                    tileEvent.tile.ReturnGrid();
                }
                else
                {

                    if (EnterGrid.Tile != null)
                    {
                        //EnterGrid.Tile.ToGrid();
                        EnterGrid.Tile.ToGrid(tileEvent.tile.Grid);
                    }

                    tileEvent.tile.ToGrid(EnterGrid);


                }

            }
        }

    }
}